<?php
namespace app\home\controller;

use app\admin\model\User;
use think\Controller;
use think\Cookie;
use think\Db;
use think\Request;
use think\Session;
use ucpaas\Ucpaas;

class Login extends Controller
{
    //登录页
    public function getIndex()
    {
        return $this->fetch('login/index');
    }
    //登录方法
    public function postLogin(){
        $user_name=input('post.user_name');
        $user_pass=md5(input('post.user_pass'));
        $remember=input('post.remember');
        //首先进行用户名判断
        $user=new User();
        //先判断用户是否被禁用
        $status=$user->where('user_name',$user_name)->value('status');
        if($status==1){
            //表示用户已被禁用，就提示无法登录
            $data=[
                'status' =>2,
                'msg'   =>'用户已被禁用，暂时无法使用',
            ];
            return $data;
        }
        $pass=$user->where('user_name',$user_name)->value('user_pass');
        $id=$user->where('user_name',$user_name)->value('id');
        if($pass==$user_pass){
            //首先判断是否点击了保存密码，保存了就将信息（账号和密码）保存到Cookie
            $userinfo=[
                'user_id'   =>$id,
                'user_name' =>$user_name,
            ];
            if($remember=='on'){
                //保存里面打开，就把信息存入到本地的cookie
                Cookie::forever('userinfo',$userinfo);
            }
            //登录成功来就存入Session;
                Session::set('userinfo',$userinfo);
            $data=[
                'status' =>0,
                'msg'   =>'登录成功',
            ];
        }else{
            $data=[
                'status' =>1,
                'msg'   =>'用户名或密码错误',
            ];
        }
        return $data;
    }
    //退出
    public function getLogout(){
        //不仅仅要去除session的状态  也要去除cookie
        Session::delete('userinfo');
        Cookie::delete('userinfo');
        $this->redirect('/');
    }
    //注册
    public function getCreate(){
        return $this->fetch('login/create');
    }

    //保存用户
    public function postSave(Request $request){
        //先获取手机验证码，进行调用方法的验证
        $req=$request->except(['action','reuser_pass','phone_code','user_pass']);
        $req['user_pass']=md5(input('post.user_pass'));
        $user_name=$req['user_name'];
        //如果验证码验证成功，进行下一步
        if($req['user_phone']!=null)
        {
            unset($req['user_email']);
            unset($req['email_code']);
            $code=Session::get('phone_code');
            if($code != input('post.phone_code')){
                //如果传过来的验证码不正确，就返回数据
                $data=[
                    'status'=>1,
                    'msg'   =>'验证码不正确，请重新输入',
                ];
                return json($data);
            }
        }else{
            unset($req['user_phone']);
            $codes=Session::get('emaile_code');
            if($codes != input('post.phone_code')){
                //如果传过来的验证码不正确，就返回数据
                $data=[
                    'status'=>1,
                    'msg'   =>'验证码不正确，请重新输入',
                ];
                return json($data);
            }

        }
        unset($req['email_code']);
        $res=Db::name('user')->insert($req);
        $id=Db::name('user')->getLastInsID();

        if($res){
            //用户创建成功就自动登录
            $userinfo=[
                'user_id'   =>$id,
                'user_name' =>$user_name,
            ];
            Session::set('userinfo',$userinfo);

            $data=[
                'status' => 0,
                'msg'    => '用户创建成功',
            ];
        }else{
            $data=[
                'status' => 2,
                'msg'    => '用户创建失败，请稍后重试',
            ];
        }
        return json($data);
    }
    //用户名是否重复的检测
    public function getUser(Request $request){
        $user=$request->except('action');
        $res=Db::name('user')->where($user)->find();
        if($res){
            $data=[
                'status' => 1,
                'msg'   => '用户名已存在，请重新输入',
            ];
        }else{
            $data=[
                'status' => 0,
                'msg'   => '用户名可用',
            ];
        }
        return json($data);
    }

    //邮箱激活
    public function postemail()
    {
        $request=$this->request;
        $res=$request->param();
        $s=send_email($res['user_email'],"用户激活","<a href='http://www.thinkphp5.com/home/login/activation'>激活当前用户</a>");
        if($s)
        {
            $data=[
                'status' => 0,
                'msg'   => '激活用户邮件发送成功，请登录你的邮箱激活用户',
            ];
            return $data;
        }
    }
    //邮箱验证
    public function getactivation()
    {
        $s=rand(000000,999999);
        $email_code=Session::set('email_code',$s);
        echo $s;
    }

    //手机验证
   public function postphone()
   {
       $request=$this->request;
       $res=$request->param();
       $checks=check_phones($res['phone'],$res['s']);
       $codes=Session::set('phone_code',$res['s']);
       return $checks;
   }
}
